#!/bin/bash

# 测试视频下载系统启动脚本
# 支持Docker Compose一键启动所有服务

echo "=========================================="
echo "测试视频下载系统 - 微服务架构"
echo "=========================================="

# 检查Docker和Docker Compose是否安装
if ! command -v docker &> /dev/null; then
    echo "❌ Docker未安装，请先安装Docker"
    exit 1
fi

if ! command -v docker-compose &> /dev/null; then
    echo "❌ Docker Compose未安装，请先安装Docker Compose"
    exit 1
fi

# 创建必要的目录
echo "📁 创建必要的目录..."
mkdir -p logs
mkdir -p data/mysql
mkdir -p data/redis
mkdir -p data/rabbitmq
mkdir -p downloads
mkdir -p files

# 设置权限
chmod 755 logs downloads files
chmod 777 data/mysql data/redis data/rabbitmq

echo "🔧 启动基础设施服务..."
docker-compose up -d mysql redis rabbitmq nacos

echo "⏳ 等待基础设施服务启动完成..."
sleep 30

echo "🚀 启动微服务..."
docker-compose up -d gateway-service auth-service download-service parser-service file-service

echo "🌐 启动前端服务..."
docker-compose up -d frontend

echo "✅ 所有服务启动完成！"
echo ""
echo "📋 服务访问地址："
echo "  前端界面:     http://localhost:3000"
echo "  API网关:      http://localhost:8080"
echo "  认证服务:     http://localhost:8081"
echo "  下载服务:     http://localhost:8082"
echo "  解析服务:     http://localhost:8083"
echo "  文件服务:     http://localhost:8084"
echo "  Nacos控制台:  http://localhost:8848/nacos (用户名/密码: nacos/nacos)"
echo ""
echo "📊 监控地址："
echo "  MySQL:        localhost:3306 (用户名/密码: root/123456)"
echo "  Redis:        localhost:6379"
echo "  RabbitMQ:     http://localhost:15672 (用户名/密码: admin/admin123)"
echo ""
echo "🔍 查看服务状态: docker-compose ps"
echo "📝 查看服务日志: docker-compose logs -f [服务名]"
echo "🛑 停止所有服务: docker-compose down"
echo "=========================================="